//
//  SettingViewController.swift
//  Box
//
//  Created by mn on 2025/3/31.
//
import UIKit

enum SettingOptions: String,CaseIterable {
    case aboutUs = "关于我们"
    case privacyPolicy = "隐私政策"
    case serviceAgreement = "服务协议"
    case closeAccount = "注销账户"
    case logOut = "退出登录"
}


class SettingViewController: BaseViewController, UITableViewDataSource,UITableViewDelegate {
    private let tableView = UITableView()
    let titles = SettingOptions.allCases
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "设置"
        tableView.backgroundColor = .fromHex("#F4F6F8")

        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(nav.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }

        tableView.bounces = false
        tableView.dataSource = self
        tableView.delegate = self
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorColor = .clear
        tableView.rowHeight = 53
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        titles.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = titles[indexPath.row].rawValue
        cell.textLabel?.textColor = .black
        cell.textLabel?.font = UIFont.systemFont(ofSize: 15,weight: .bold)
        cell.backgroundColor = .clear
        let arrowImageView = UIImageView(image: UIImage.arrowRightGray)
        arrowImageView.contentMode = .scaleAspectFit
        cell.selectionStyle = .none
        cell.accessoryView = arrowImageView
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch titles[indexPath.row] {
        case .logOut:
            logOut()
        case .closeAccount:
            deleteUser()
        case .aboutUs:
            openWeb("https://home.cqbbx.cn/web/agreement/aboutUs.html")
        case .privacyPolicy:
            openWeb("https://home.cqbbx.cn/web/agreement/privacyPolicy.html")
        case .serviceAgreement:
            openWeb("https://home.cqbbx.cn/web/agreement/serviceAgreement.html")
        }
    }
    func openWeb(_ url: String) {
        let vc = WebViewController(url:url)
        navigationController?.push(vc)
    }
    func logOut() {
        let alert = UIAlertController(title: "确认退出登录？", message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        alert.addAction(UIAlertAction(title: "确认", style: .destructive, handler: { [weak self] _ in
            APIService.request(api: .logout) { [weak self] (result, _) in
                if result.success {
                    UserManager.shared.clearUser()
                    self?.navigationController?.pop()
                } else {
                    self?.toast(result.message)
                }
            }
        }))
        present(alert, animated: true, completion: nil)
    }
    func deleteUser() {
        let waringVC = WaringViewController()
        waringVC.modalPresentationStyle = .overFullScreen
        present(waringVC, animated: true, completion: nil)
    }

}
